﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Runtime.Serialization;
[Serializable]
public unsafe class Colores : IXmlSerializable
{
    private float[] _colores;
    public Colores()
    {
        _colores = new float[12];
    }
    public Colores(int r, int v, int a)
    {
        _colores = new float[12];
        this.Azul = a;
        this.Rojo = r;
        this.Verde = v;
        this.Alfa = 255;
        this.setColores();
    }

    public Colores(int r, int v, int a, int f)
    {
        _colores = new float[12];
        this.Azul = a;
        this.Rojo = r;
        this.Verde = v;
        this.Alfa = f;
        this.setColores();
    }

    public Colores(int intIndiceDXF)
    {
        this.Azul = intIndiceDXF;
        this.Rojo = 50;
        this.Verde = 200;
        this.Alfa = 255;
        this.setColores();
    }

    public Colores(Color objColorWindows)
    {
        _colores = new float[12];
        this.Azul = objColorWindows.B;
        this.Rojo = objColorWindows.R;
        this.Verde = objColorWindows.G;
        this.Alfa = objColorWindows.A;
        this.setColores();
    }

    public void SetOGL(float r, float v, float a)
    {
        this._azul = a;
        this._rojo = r;
        this._verde = v;
        this._alfa = 1.0f;
        this.setColores();
    }

    public void SetOGL(float r, float v, float a, float t)
    {
        this._azul = a;
        this._rojo = r;
        this._verde = v;
        this._alfa = t;
        this.setColores();
    }

    public Color getWindowsColor()
    {
        return Color.FromArgb((int)(this._rojo * 255), (int)(this._verde * 255), (int)(this._azul * 255));
    }

    private float _rojo;
    public float Rojo
    {
        get { return _rojo; }
        set
        {
            if ((value < 0))
            {
                _rojo = 0;
            }
            else if ((value > 255))
            {
                _rojo = 1;
            }
            else
            {
                _rojo = value / 255;
            }
            this.setColores();
        }
    }

    private float _verde;
    public float Verde
    {
        get { return _verde; }
        set
        {
            if ((value < 0))
            {
                _verde = 0;
            }
            else if ((value > 255))
            {
                _verde = 1;
            }
            else
            {
                _verde = value / 255;
            }
            this.setColores();
        }
    }

    private float _azul;
    public float Azul
    {
        get { return _azul; }
        set
        {
            if ((value < 0))
            {
                _azul = 0;
            }
            else if ((value > 255))
            {
                _azul = 1;
            }
            else
            {
                _azul = value / 255;
            }
            this.setColores();
        }
    }

    public float* ArregloColores()
    {
        fixed (float* apuntaColor = &_colores[0])
        {
            return apuntaColor;
        }
    }

    private void setColores()
    {
        this._colores[0] = this.Rojo;
        this._colores[1] = this.Verde;
        this._colores[2] = this.Azul;
        this._colores[3] = this.Alfa;
        this._colores[4] = this.Rojo;
        this._colores[5] = this.Verde;
        this._colores[6] = this.Azul;
        this._colores[7] = this.Alfa;
        this._colores[8] = this.Rojo;
        this._colores[9] = this.Verde;
        this._colores[10] = this.Azul;
        this._colores[11] = this.Alfa;
    }

    private float _alfa;
    public float Alfa
    {
        get { return _alfa; }
        set
        {
            if ((value < 0))
            {
                _alfa = 0;
            }
            else if ((value > 255))
            {
                _alfa = 1;
            }
            else
            {
                _alfa = value / 255;
            }
            this.setColores();
        }
    }

    public override string ToString()
    {
        return "Color";
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteAttributeString("r", this._rojo.ToString());
        writer.WriteAttributeString("v", this._verde.ToString());
        writer.WriteAttributeString("a", this._azul.ToString());
        writer.WriteAttributeString("f", this._alfa.ToString());
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.MoveToContent();
        this._rojo = float.Parse(reader.GetAttribute("r"));
        this._verde = float.Parse(reader.GetAttribute("v"));
        this._azul = float.Parse(reader.GetAttribute("a"));
        this._alfa = float.Parse(reader.GetAttribute("f"));
        this.setColores();
    }
    public XmlSchema GetSchema()
    {
        return null;
    }

}
